package com.wlz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/demo")
public class Demo {

    @Autowired
    private DiscoveryClient discoveryClient;

    @ResponseBody
    @GetMapping("/gets")
    public String getServer(){
        List<String> services = discoveryClient.getServices();
        //获取指定服务的集合(实际中可能会有集群)
        List<ServiceInstance> instances = discoveryClient.getInstances("order-service");

        //这里只有一个，获取那一个服务就好
        return services.toString()+" instances num = "+instances.size();
    }

}
